﻿//-----------------------------------------------------------------------
// <copyright file="DoNotUseBannedApisConfiguration.cs">
//     Copyright (c) Nicole Calinoiu. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

using Microsoft.FxCop.Sdk;

using Bordecal.FxCop.Sdk;

namespace Bordecal.FxCop.Rules.Usage.Configuration
{
	/// <summary>
	/// Configuration for the <see cref="DoNotUseBannedApis"/> rule.
	/// </summary>
	public sealed class DoNotUseBannedApisConfiguration : RuleConfiguration
	{
		private const char Separator = '|';

		/// <summary>
		/// Initializes a new instance of the <see cref="DoNotUseBannedApisConfiguration"/> class.
		/// </summary>
		public DoNotUseBannedApisConfiguration()
		{
			this.BannedTypes = new string[]
				{
					"System.Diagnostics.Debug"
				};

			this.BannedMembers = new string[]
				{
					"System.GC.Collect",
					"System.GC.Collect(System.Int32)",
					"System.GC.Collect(System.Int32,System.GCCollectionMode)",
					"System.Threading.Thread.Abort",
					"System.Threading.Thread.Abort(System.Object)"
				};

			this.InitializeSettings();
		}

		/// <summary>
		/// Gets the banned type list.
		/// </summary>
		internal string[] BannedTypes { get; private set; }

		/// <summary>
		/// Gets the banned member list.
		/// </summary>
		internal string[] BannedMembers { get; private set; }

		/// <summary>
		/// Copies settings from instance properties to a string dictionary.
		/// </summary>
		/// <param name="dictionary">The target settings <see cref="Dictionary{String, String}"/>.</param>
		protected override void DoCopySettings(Dictionary<string, string> dictionary)
		{
			if (dictionary == null)
			{
				throw new ArgumentNullException("dictionary");
			}

			string separator = DoNotUseBannedApisConfiguration.Separator.ToString();

			dictionary.Add("BannedTypes", string.Join(separator, this.BannedTypes));
			dictionary.Add("BannedMembers", string.Join(separator, this.BannedMembers));
		}

		/// <summary>
		/// Sets configuration property values from the contents of a string dictionary.
		/// </summary>
		/// <param name="dictionary">The source <see cref="Dictionary{String, String}"/>.</param>
		protected override void DoLoadFromDictionary(Dictionary<string, string> dictionary)
		{
			if (dictionary == null)
			{
				throw new ArgumentNullException("dictionary");
			}

			char separator = DoNotUseBannedApisConfiguration.Separator;

			this.BannedTypes = dictionary["BannedTypes"].Split(separator);
			this.BannedMembers = dictionary["BannedMembers"].Split(separator);
		}
	}
}